package com.dyh.quickdemo.demos.lbdand;

import com.dyh.quickdemo.bizDemo.MaxLbd;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.util.Random;
import java.util.function.Function;
import java.util.function.Predicate;

/*
@Author=Joey Wheeler
@Date=2020/12/4 17:12
*/
public class Demo {

    @Test
    public void test(){
        CircuitBreaker.node(
            "hello",
                ()->{
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("hello!");
                }
        ).work();
    }


    @Test
    public void test1(){
        Random random = new Random();
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
    }
    /*
    Random random = new Random();
    */

    @Test
    public void test2() throws InterruptedException {
        TaobaoClient taobaoClient = new TaobaoClient();


        Function<BaseTaobaoReq,BaseTaobaoRsp> function =
                FcBreaker.with("helloTaobao",
                        taobaoClient::exe,fallback,fallbackTrigger);


//
        function.apply(new BaseTaobaoReq());
        function.apply(new BaseTaobaoReq());
//        function.apply(BaseTaobaoReq.expire());
//        function.apply(BaseTaobaoReq.expire());
//        function.apply(BaseTaobaoReq.expire());

        // function.apply(new BaseTaobaoReq());
      //  FcBreakerCenter.endStatistics("helloTaobao");


        Thread.sleep(2000L);
    }

    Function<BaseTaobaoReq,BaseTaobaoRsp> fallback = e -> {
        BaseTaobaoRsp rsp = new BaseTaobaoRsp();
        rsp.setMsg("授权过期");
        rsp.setSuccess(false);
        return rsp;
    };

    Predicate<BaseTaobaoRsp> fallbackTrigger = e-> !StringUtils.isEmpty(e.getMsg())&& e.getMsg().contains("授权过期");

}
